package com.dale.controller;

import cn.hutool.core.map.MapUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dale.common.dto.HotCollectionDto;
import com.dale.common.lang.Result;
import com.dale.entity.Blog;
import com.dale.entity.CollectBlog;
import com.dale.entity.HotCollections;
import com.dale.service.BlogService;
import com.dale.service.CollectBlogService;
import com.dale.service.HotCollectionsService;
import com.dale.util.ShiroUtil;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Gyt
 * @create 2021-06-04 - 17:27
 */

/**
 * 管理 博客收藏
 */
@RestController
public class CollectBlogController {

    @Autowired
    BlogService blogService;

    @Autowired
    CollectBlogService collectBlogService;

    @Autowired
    HotCollectionsService hotCollectionsService;

    /**
     * 收藏博客
     */
    @RequiresAuthentication
    @PostMapping("/addBlogCollection")
    @RequiresRoles(value = {"common", "admin"}, logical = Logical.OR)
    public Result addBlogCollection(@RequestParam Long bid) {

        //收藏过就禁止收藏
        CollectBlog one = collectBlogService.getOne(new QueryWrapper<CollectBlog>()
                .eq("uid", ShiroUtil.getProfile().getId())
                .eq("bid", bid));

        Assert.isTrue(one == null, "重复收藏错误");

        CollectBlog collectBlog = new CollectBlog();
        collectBlog.setBid(bid);
        collectBlog.setUid(ShiroUtil.getProfile().getId());
        collectBlog.setCreated(LocalDateTime.now());
        collectBlogService.save(collectBlog);

        return Result.succ(null);
    }

    /**
     * 展示个人收藏
     */
    @RequiresAuthentication
    @PostMapping("/showMyCollection")
    @RequiresRoles(value = {"common", "admin"}, logical = Logical.OR)
    public Result showMyCollection() {

        //根据用户id 查找用户收藏的博客
        List<CollectBlog> collectBlogs = collectBlogService.list(new QueryWrapper<CollectBlog>()
                .eq("uid", ShiroUtil.getProfile().getId())
                .orderByDesc("created")
        );

        ArrayList<Blog> blogs = new ArrayList<>();
        for (CollectBlog collectBlog : collectBlogs) {
            Blog byId = blogService.getOne(new QueryWrapper<Blog>().eq("id", collectBlog.getBid()));
            if (byId != null)
                blogs.add(byId);
        }

        //将用户收藏的文章返回
        return Result.succ(blogs);
    }


    @RequiresAuthentication
    @DeleteMapping("delCollection")
    @RequiresRoles(value = {"common", "admin"}, logical = Logical.OR)
    public Result delCollection(@RequestParam Long bid) {
        QueryWrapper<CollectBlog> eq = new QueryWrapper<CollectBlog>().eq("uid", ShiroUtil.getProfile().getId()).eq("bid", bid);

        CollectBlog byId = collectBlogService.getOne(eq);
        Assert.isTrue(byId != null, "未曾收藏该文章错误");

        collectBlogService.remove(eq);

        return Result.succ(null);

    }


    /**
     * 返回是否收藏
     */
    @RequiresAuthentication
    @GetMapping("/isCollected/{bid}")
    @RequiresRoles(value = {"common", "admin"}, logical = Logical.OR)     //表示是普通用户 或者 admin用户可访问
    public Result isCollected(@PathVariable Long bid) {
        CollectBlog one = collectBlogService.getOne(new QueryWrapper<CollectBlog>().eq("bid", bid).eq("uid", ShiroUtil.getProfile().getId()));

        if (one == null) {
            //未收藏过
            return Result.succ(MapUtil.builder()
                    .put("isCollected", false)
                    .map());
        } else {
            //收藏过
            return Result.succ(MapUtil.builder()
                    .put("isCollected", true)
                    .map());
        }

    }


    /**
     * 添加到热门收藏夹(分享收藏)
     */

    @PostMapping("/shareMyCollections")
    @RequiresAuthentication
    @RequiresRoles(value = {"common", "admin"}, logical = Logical.OR)
    public Result shareMyCollections(@RequestParam String description) {
        Long uid = ShiroUtil.getProfile().getId();

        //如果已经设置了(判断uid)
        HotCollections uid1 = hotCollectionsService.getOne(new QueryWrapper<HotCollections>().eq("uid", uid));
        Assert.isTrue(uid1 == null, "您已经分享了自己的收藏，若要取消收藏分享请联系管理员");

        //将其装入数据库 并等待管理员通过
        HotCollections hotCollections = new HotCollections();
        hotCollections.setDescription(description);
        hotCollections.setUid(uid);
        hotCollections.setIsPass(0);
        //数据库的形式为 表名 hot_collections   通过uid来判断是否分享   is_pass是否通过管理员认证  分享理由

        hotCollectionsService.save(hotCollections);

        return Result.succ(null);
    }

    /**
     * 添加到热门收藏夹(分享收藏)
     */

    @PostMapping("/noShareMyCollections")
    @RequiresAuthentication
    @RequiresRoles(value = {"common", "admin"}, logical = Logical.OR)
    public Result noShareMyCollections() {
        Long uid = ShiroUtil.getProfile().getId();

        //如果已经设置了(判断uid)
        HotCollections uid1 = hotCollectionsService.getOne(new QueryWrapper<HotCollections>().eq("uid", uid));
        Assert.isTrue(uid1 != null, "你未曾分享您的收藏夹，请先分享");



        hotCollectionsService.removeById(uid1.getHcid());

        return Result.succ(null);
    }


    /**
     * 展示热门收藏的内容
     * <p>
     * 包括用户id
     * 用户的收藏
     * 用户收藏想法
     *
     * @return
     */
    @GetMapping("/getHotCollections")
    public Result getHotCollections() {
        List<HotCollectionDto> hotCollections = hotCollectionsService.getHotCollections();

        return Result.succ(hotCollections);
    }


    @GetMapping("/getHotCollectionsByAdmin")
    @RequiresRoles("admin")
    @RequiresAuthentication
    public Result getHotCollectionsByAdmin() {
        List<HotCollectionDto> hotCollections = hotCollectionsService.getHotCollectionsByAdmin();

        return Result.succ(hotCollections);
    }


    /**
     * 让该收藏夹通过
     * @return
     */
    @PostMapping("/passCollection")
    @RequiresAuthentication
    @RequiresRoles("admin")
    public Result passCollection(@RequestParam Long hcid) {
        //查到该热门收藏
        HotCollections byId = hotCollectionsService.getById(hcid);

        //设置为通过或不通过
        if (byId.getIsPass() == 0) {
            byId.setIsPass(1);
        } else {
            byId.setIsPass(0);
        }

        hotCollectionsService.updateById(byId);

        return Result.succ(null);
    }

}
